﻿#if DEBUG
#define CODE_AUTO
#endif


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using Top_Meeting.UI;
using TopVisionVideoStream;

namespace Top_Meeting.Window
{
    public partial class Frm_Login : Frm_Base
    {
        #region 字段

        private string code;

        private bool isLoginLayout = true;

        Point pointLoginErrTip = new Point(377, 320);
        Point pointRegistErrTip = new Point(490, 344);

        #endregion


        #region Initialize

        public Frm_Login()
        {
            InitializeComponent();
            FrmInit();
        }

        #endregion


        #region UI

        #region OVERRIDE_METHOD

        protected override void OnLoad(EventArgs e)
        {
            LoginLayout();

            base.OnLoad(e);
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            if (!isLoginLayout)
            {
                e.Cancel = true;
                LoginLayout();
            }

            base.OnClosing(e);
        }

        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);

            Frm_Startup.instance.Close();
        }

        protected override void UpdateLayout()
        {
            base.UpdateLayout();

            layPanel_Main.Parent = layPanel_Body;

            //Font

            //Form
            this.BackColor = Color.Transparent;
            layPanel_Body.ImageToBackground("img_1001_n");

            //Input

            layPanel_Account.ImageToBackground("img_1012_n");

            layPanel_Password.ImageToBackground("img_1014_n");
            layPanel_Password2.ImageToBackground("img_1014_n");

            layPanel_Code.ImageToBackground("img_1013_n");

            layBtn_Login.ImageToButtonImage("button_1001");
            layBtn_Login.PressedImage = TMUI.SkinImage("button_1001_c");

            layPanel_Input_Context.SizeToChild(1);
            layPanel_Input_Context.Align(0);
            layPanel_Input_Context.Top = 0;

            //Code
            layPanel_CodeNum.Cursor = Cursors.Hand;
            RefreshCode();

            //Register
            layLab_AutoLogin.Cursor = Cursors.Hand;
            layCB_AutoLogin.Cursor = Cursors.Hand;
            layLab_AutoLogin.Click += LayLab_AutoLogin_Click;
            layLBtn_Register.Cursor = Cursors.Hand;
            layLBtn_Forget.Cursor = Cursors.Hand;

            //Logo
            layPanel_LogoImg.ImageToBackground("img_1011_n");

            Helper.SizeToBG(layPanel_Account,
                            layPanel_Password,
                            layPanel_Password2,
                            layPanel_Code,
                            layBtn_Login,
                            layPanel_LogoImg);

            Helper.Align(0,
                        layPanel_Account,
                        layPanel_Password,
                        layPanel_Password2,
                        layPanel_Code,
                        layBtn_Login,
                        layPanel_LogoImg);

            //Tip
            layPanel_Tip_Err.ImageToBackground("img_1009_n");
            layPanel_Tip_Err.SizeToBG();

            layPanel_Tip_Err_Board.ImageToBackground("img_1010_n");
            layPanel_Tip_Err_Board.SizeToBG();
            layPanel_PasswordBarBG.ImageToBackground("img_1005_n");
            layPanel_PasswordBarBG.SizeToBG();
            layPanel_PasswordBarBG.Align(0);

            //Control box
            SetControlBoxButton(false, false, true);
        }

        protected override void InitEvent()
        {
            base.InitEvent();

            //Form


            //Control
            layPanel_Main.MouseDown += Frm_MouseDown;
            layPanel_Logo.MouseDown += Frm_MouseDown;
            layPanel_Input_Context.MouseDown += Frm_MouseDown;
            foreach (Control item in layPanel_Input_Context.Controls)
            {
                item.MouseDown += Frm_MouseDown;
                //item.Enter += (o, e) => { item.BackColor = Color.FromArgb(50, 0, 0, 0); };
                //item.Leave += (o, e) => { item.BackColor = Color.Transparent; };
            }
            layPanel_Oper.MouseDown += Frm_MouseDown;
            layPanel_Input.MouseDown += Frm_MouseDown;
            layPanel_Login.MouseDown += Frm_MouseDown;
            layPanel_AutoLogin.MouseDown += Frm_MouseDown;
            layPanel_Regist.MouseDown += Frm_MouseDown;
            layPanel_Logo.MouseDown += Frm_MouseDown;
            layPanel_LogoImg.MouseDown += Frm_MouseDown;

            layPanel_CodeNum.Click += LayPanel_CodeNum_Click;
            layLBtn_Forget.Click += LayLBtn_Forget_Click;
            layLBtn_Register.Click += LayLBtn_Register_Click;

            layBtn_Login.Click += LayBtn_Login_Click;

            layTB_Account.TextChanged += LayTB_Account_TextChanged;

            //Control Box

            //Test
        }

        #endregion


        #region PRIVATE_METHOD

        private void RefreshCode()
        {
            code = Helper.RefreshCode(layPanel_CodeNum).Replace(" ", "");

#if CODE_AUTO
            layTB_Code.Text = code;
            layTB_Account.Text = "1576078";
            layTB_Password.Text = "itowddtl";
#endif
        }

        private void LoginLayout()
        {
            layBtn_Login.ImageToNormalImage("button_1001_n");
            layBtn_Login.HoverImage = TMUI.SkinImage("button_1001_h");
            layBtn_Login.PressedImage = TMUI.SkinImage("button_1001_c");
            layPanel_Item_Password2.Hide();
            layPanel_Oper.Show();
            isLoginLayout = true;

            layPanel_Tip_Err.Hide();
            layLab_Tip_LoginErr.Text = Configuration.Tip_Login_Err;

            layPanel_Tip_Err_Board.Hide();

            layPanel_Main.Hide();
            layPanel_Main.Show();
        }

        private void RegisterLayout()
        {
            layBtn_Login.ImageToNormalImage("button_1002_n");
            layBtn_Login.HoverImage = TMUI.SkinImage("button_1002_h");
            layBtn_Login.PressedImage = TMUI.SkinImage("button_1002_c");
            layPanel_Item_Password2.Show();
            layPanel_Oper.Hide();
            isLoginLayout = false;

            layPanel_Tip_Err.Hide();
            layLab_Tip_LoginErr.Text = Configuration.Tip_Login_Err;

            layPanel_Tip_Err_Board.Hide();

            layPanel_Main.Hide();
            layPanel_Main.Show();
        }

        private void ShowLoginErr(string err)
        {
            this.Controls.SetChildIndex(layPanel_Tip_Err, 0);
            this.Controls.SetChildIndex(layPanel_Tip_Err_Board, 1);
            layPanel_Tip_Err.Location = pointLoginErrTip;
            layPanel_Tip_Err.Show();
            layLab_Tip_LoginErr.Text = err;
        }

        private void ShowRegisterPwdErr(bool safe = true)
        {
            if (safe)
            {
                layLab_Tip_Err_Board_title.TextShowMode = LayeredSkin.TextShowModes.Ordinary;
                layLab_Tip_Err_Board.TextShowMode = LayeredSkin.TextShowModes.Ordinary;
                layLab_Tip_Err_Board_title.ForeColor = Color.FromArgb(111, 111, 111);
                layLab_Tip_Err_Board.ForeColor = Color.FromArgb(111, 111, 111);

                layLab_Tip_Err_Board_title.Text = "强度：安全";
                layLab_Tip_Err_Board.Text = "密码设置已进入安全状态，您可以继续使用。";
                layPanel_Tip_Err_Board.ImageToBackground("img_1008_n");
                layPanel_Tip_Err_Board.SizeToBG();
                layPanel_PasswordBar.BaseColor = Color.FromArgb(128, 237, 94);
                layPanel_PasswordBar.Size = new Size(layPanel_PasswordBarBG.Width, layPanel_PasswordBar.Height);
            }
            else
            {
                layLab_Tip_Err_Board_title.TextShowMode = LayeredSkin.TextShowModes.Halo;
                layLab_Tip_Err_Board.TextShowMode = LayeredSkin.TextShowModes.Halo;
                layLab_Tip_Err_Board_title.ForeColor = Color.FromArgb(255, 255, 255);
                layLab_Tip_Err_Board.ForeColor = Color.FromArgb(255, 255, 255);

                layLab_Tip_Err_Board_title.Text = "强度：太短";
                layLab_Tip_Err_Board.Text = "请至少输入6个字符。请不要使用容易被猜到的密码。";
                layPanel_Tip_Err_Board.ImageToBackground("img_1010_n");
                layPanel_PasswordBar.BaseColor = Color.FromArgb(253, 179, 43);
                layPanel_PasswordBar.Size = new Size(layPanel_PasswordBarBG.Width / 3, layPanel_PasswordBar.Height);
            }
            this.Controls.SetChildIndex(layPanel_Tip_Err, 1);
            this.Controls.SetChildIndex(layPanel_Tip_Err_Board, 0);
            layPanel_Tip_Err_Board.Location = pointRegistErrTip;
            layPanel_Tip_Err_Board.Show();
        }

        #endregion


        #region CONTROL_EVENT

        private void LayBtn_Login_Click(object sender, EventArgs e)
        {
            if (isLoginLayout)
            {
                Login();
            }
        }

        //Code

        private void LayPanel_CodeNum_Click(object sender, EventArgs e)
        {
            RefreshCode();
        }

        //Auto Log in

        private void LayLab_AutoLogin_Click(object sender, EventArgs e)
        {
            var value = layCB_AutoLogin.Checked;
            SetAutoLogin(!value);
        }

        private void SetAutoLogin(bool value)
        {
            layCB_AutoLogin.Checked = value;
        }

        private void LayLBtn_Forget_Click(object sender, EventArgs e)
        {

        }

        private void LayLBtn_Register_Click(object sender, EventArgs e)
        {
            if (isLoginLayout)
            {
                RegisterLayout();
            }
            else
            {
                LoginLayout();
            }
        }

        private void LayTB_Account_TextChanged(object sender, EventArgs e)
        {
            layPanel_Tip_Err.Hide();
        }


        #endregion



        #endregion


        #region CLIENT

        #region PRIVATE_METHOD

        private void Login()
        {
            string _account = layTB_Account.Text.Trim();
            string _password = layTB_Password.Text.Trim();
            string _code = layTB_Code.Text.Trim().ToUpper();

            if (Helper.CheckCallID(_account) == 1)
            {
                layTB_Account.Focus();
                ShowLoginErr("账号不能为空");
                return;
            }

            if (_code != code)
            {
                layTB_Code.Focus();
                ShowLoginErr("验证码错误");
                return;
            }

            TVVS.Login(_account, _password, LoginResult);

        }

        private void LoginSucess(string _msg = "")
        {
            Hide();

            Helper.ShowWindow<Frm_Main>();
        }

        private void LoginFaild(string _msg = "")
        {
            ShowLoginErr(Configuration.Tip_Login_Err);

            Debug.WriteLine("login err:" + _msg);
        }

        #endregion

        #region EVENT

        private void LoginResult(bool isScucess, int msg)
        {
            if (isScucess)
            {
                LoginSucess();
            }
            else
            {
                LoginFaild();
            }
        }

        #endregion


        #endregion
    }
}
